package kr.hkit.androidcomplete02.ch19;

import kr.hkit.androidcomplete02.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.TextView;

public class LongTime3 extends Activity {
	int mValue;
	TextView tv;
	boolean mQuit;
	ProgressDialog mProgress;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch19_uploadex);
		
		tv = (TextView) findViewById(R.id.ch19_uploadex_tv);
	}
	
	Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			mValue++;
			tv.setText(Integer.toString(mValue));
			
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			
			if(mValue < 100 && mQuit == false){
				mProgress.setProgress(mValue);
				mHandler.sendEmptyMessage(0);
			} else {
				mProgress.dismiss();
			}
		};
	};
	
	public void mOnClick(View v){
		if(mValue == 100) mValue = 0;
		mProgress = new ProgressDialog(this);
		mProgress.setTitle("Updating");
		mProgress.setMessage("Wait...");
		mProgress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
		mProgress.setOnCancelListener(new OnCancelListener() {
			public void onCancel(DialogInterface dialog) {
				mQuit = true;
				mProgress.dismiss();
			}
		});
		mProgress.show();
		mQuit = false;
		mHandler.sendEmptyMessage(0);
	}
}
